﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.ToChucHanhChinh.UserControl
{
    public partial class AddToChucHanhChinh : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        
        private void LoadDataForControl()
        {
            //Loai To Chuc
            storeLoaiToChuc.DataSource = (new CommonCode()).LoadDataCommonByCodeKey("LoaiDonViHanhChinh");
            storeLoaiToChuc.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pType">AddNew,View,Edit</param>
        /// <param name="pToChucChaID"></param>
        public void ShowDialog(string type, int pToChucID)
        {
            this.hdfType.Text = type;
            LoadDataForControl();
            //AddNew
            wdAddToChucHanhChinh.Show();
            if (type.Equals("AddNew"))
            {
                ResetControl();
                this.hdfToChucHanhChinhIDCha.Text = pToChucID.ToString();
                this.wdAddToChucHanhChinh.Icon = Icon.Add;
                this.wdAddToChucHanhChinh.Title = "Tạo mới tổ chức";
                this.cboLoaiToChuc.ReadOnly = false;
                this.cboLoaiToChuc.SelectedIndex = 0;
                this.btnSave.Hidden = false;
            }
            else if (type.Equals("View"))
            {
                this.wdAddToChucHanhChinh.Icon = Icon.ApplicationGo;
                this.wdAddToChucHanhChinh.Title = "Chi tiết tổ chức";
                this.btnSave.Hidden = true;
                LoadDataView(pToChucID);
                this.txtTenToChucHanhChinh.ReadOnly = true;
                this.txtGhiChu.ReadOnly = true;
                this.cboLoaiToChuc.ReadOnly = true;
            }
            else if (type.Equals("Edit"))
            {
                this.wdAddToChucHanhChinh.Icon = Icon.ApplicationEdit;
                this.wdAddToChucHanhChinh.Title = "Sửa tổ chức";
                this.btnSave.Hidden = false;
                this.txtTenToChucHanhChinh.ReadOnly = false;
                this.txtGhiChu.ReadOnly = false;
                this.cboLoaiToChuc.ReadOnly = false;
                LoadDataView(pToChucID);
            }
            if (hdfToChucHanhChinhIDCha.Text == "0")
            {

                this.txtToChucHanhChinhTrucThuoc.Text = "Trung ương";
                this.hdfToChucHanhChinhLevel.Text = "0";
            }
            else
            {
                var oNSToChucCha = (new CHToChucHanhChinh()).GetByID(int.Parse(hdfToChucHanhChinhIDCha.Text));
                this.txtToChucHanhChinhTrucThuoc.Text = oNSToChucCha.TenToChucHanhChinh;
                this.hdfToChucHanhChinhLevel.Text = (oNSToChucCha.ToChucHanhChinhLevel + 1).ToString();
            }
           
        }

        private void LoadDataView(int id)
        {
            var oCHToChucHanhChinh = (new CHToChucHanhChinh()).GetByID(id);
           hdfToChucHanhChinhID.Text = oCHToChucHanhChinh.ToChucHanhChinhID.ToString();
           hdfToChucHanhChinhIDCha.Text = oCHToChucHanhChinh.ToChucHanhChinhIDCha.ToString();
           hdfToChucHanhChinhLevel.Text = oCHToChucHanhChinh.ToChucHanhChinhLevel.ToString();
           hdfLoaiToChucHanhChinh.Text = oCHToChucHanhChinh.LoaiToChucHanhChinh.ToString();
           txtTenToChucHanhChinh.Text = oCHToChucHanhChinh.TenToChucHanhChinh;
           cboLoaiToChuc.Value = oCHToChucHanhChinh.LoaiToChucHanhChinh;
           txtGhiChu.Text = oCHToChucHanhChinh.GhiChu;
        }
        private void ResetControl()
        {
            hdfToChucHanhChinhID.Text=string.Empty;
            hdfToChucHanhChinhIDCha.Text=string.Empty;
            hdfToChucHanhChinhLevel.Text=string.Empty;
            hdfLoaiToChucHanhChinh.Text=string.Empty;
            txtTenToChucHanhChinh.Text=string.Empty;
            cboLoaiToChuc.Value = string.Empty;
            txtGhiChu.Text=string.Empty;
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                CHToChucHanhChinh oCHToChucHanhChinh;
                if (this.hdfType.Text == "AddNew")
                {
                    //Add New
                    oCHToChucHanhChinh = new CHToChucHanhChinh();
                }
                else
                {
                    //Update
                    oCHToChucHanhChinh = (new CHToChucHanhChinh()).GetByID(int.Parse(hdfToChucHanhChinhID.Text));
                }

                oCHToChucHanhChinh.ToChucHanhChinhLevel = int.Parse(hdfToChucHanhChinhLevel.Text);
                oCHToChucHanhChinh.ToChucHanhChinhIDCha = int.Parse(hdfToChucHanhChinhIDCha.Text);
                oCHToChucHanhChinh.TenToChucHanhChinh = txtTenToChucHanhChinh.Text;
                oCHToChucHanhChinh.LoaiToChucHanhChinh = int.Parse(cboLoaiToChuc.SelectedItem.Value);
                oCHToChucHanhChinh.GhiChu = txtGhiChu.Text;
                oCHToChucHanhChinh.Save();
                ((ToChucHanhChinh)(this.Page)).LoadDataForTree();
                ((ToChucHanhChinh)(this.Page)).LoadGrid(oCHToChucHanhChinh.ToChucHanhChinhIDCha);
                
                this.wdAddToChucHanhChinh.Hide();
            }
           catch (Exception)
            {
                
                throw;
            }

        }
    }
}